package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizTodayTransactionAmountMapper;
import com.cykj.pos.domain.BizTransactionAmount;
import com.cykj.pos.service.IBizTransactionAmountService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * pos1.0交易通知(今日)Service业务层处理
 *
 * @author tzf
 * @date 2025-06-17
 */
@Service
public class BizTodayTransactionAmountServiceImpl extends ServiceImpl<BizTodayTransactionAmountMapper, BizTransactionAmount> implements IBizTransactionAmountService {

    @Resource
    private BizTodayTransactionAmountMapper bizTodayTransactionAmountMapper;

    @Override
    public List<BizTransactionAmount> queryList(BizTransactionAmount bizTransactionAmount) {
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        bizTransactionAmount.setDeptId(sysUserman.getDeptId());
        if("1".equals(bizTransactionAmount.getType())){
            bizTransactionAmount.setTableName("biz_transaction_amount");
        }else if ("2".equals(bizTransactionAmount.getType())){
            bizTransactionAmount.setTableName("biz_transaction_amount_this_month");
        }else{
            bizTransactionAmount.setTableName("biz_transaction_amount_history");
        }
        Map<String, Object> params = bizTransactionAmount.getParams();
        if (params.get("beginHostDate") != null && params.get("endHostDate") != null) {
            bizTransactionAmount.setBeginDate(params.get("beginHostDate").toString()+" 00:00:00");
            bizTransactionAmount.setEndDate(params.get("endHostDate").toString()+" 23:59:59");
        }
        return bizTodayTransactionAmountMapper.selectTodayTransList(bizTransactionAmount);
    }

    @Override
    public AjaxResult executeStatistics(BizTransactionAmount bizTransactionAmount) {
        AjaxResult ajaxResult = AjaxResult.success("统计");
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        bizTransactionAmount.setDeptId(sysUserman.getDeptId());
        if("1".equals(bizTransactionAmount.getType())){
            bizTransactionAmount.setTableName("biz_transaction_amount");
        }else if ("2".equals(bizTransactionAmount.getType())){
            bizTransactionAmount.setTableName("biz_transaction_amount_this_month");
        }else{
            bizTransactionAmount.setTableName("biz_transaction_amount_history");
        }
        Map<String, Object> params = bizTransactionAmount.getParams();
        if (params.get("beginHostDate") != null && params.get("endHostDate") != null) {
            bizTransactionAmount.setBeginDate(params.get("beginHostDate").toString()+" 00:00:00");
            bizTransactionAmount.setEndDate(params.get("endHostDate").toString()+" 23:59:59");
        }
        int totalCount = bizTodayTransactionAmountMapper.executeStatisticsCount(bizTransactionAmount);
        ajaxResult.put("totalCount",totalCount);
        double totalAmount = bizTodayTransactionAmountMapper.executeStatisticsAmount(bizTransactionAmount);
        ajaxResult.put("totalAmount",totalAmount);
        double serviceFeeAmount = bizTodayTransactionAmountMapper.executeStatisticsService(bizTransactionAmount);
        ajaxResult.put("serviceFeeAmount",serviceFeeAmount);
        double flowFeeAmount = bizTodayTransactionAmountMapper.executeStatisticsFlow(bizTransactionAmount);
        ajaxResult.put("flowFeeAmount",flowFeeAmount);
        return ajaxResult;
    }
}
